package com.example.springbootmpdemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootmpdemo.entity.Active;
import com.example.springbootmpdemo.entity.Member;
import com.example.springbootmpdemo.entity.Team;
import com.example.springbootmpdemo.service.IActiveService;
import com.example.springbootmpdemo.service.IMemberService;
import com.example.springbootmpdemo.service.ITeamService;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author metasequoia
 * @since 2021-12-19
 */
@Controller
@RequestMapping("/active")
public class ActiveController {
    @Autowired
    private IActiveService iActiveService;

    @Autowired
    private ITeamService iTeamService;

    @Autowired
    private IMemberService iMemberService;

    @PostMapping("/search")
    public ModelAndView search(@RequestParam int teamid,
                               @RequestParam int memberid){

        //Wrapper是一个类似“条件”一样的东西
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("teamid",teamid);
        //根据条件查询返回一个Active类型的list
        List<Active> activeList = iActiveService.list(wrapper);

        if(!activeList.isEmpty()){

            //因为需要显示team的name，所以再来一个team对象
            Team team = iTeamService.getById(teamid);
            Member member = iMemberService.getById(memberid);

            ModelAndView mv = new ModelAndView("activeResult");
            mv.addObject("activelist",activeList);
            mv.addObject("team",team);
            mv.addObject("member",member);
            return mv;

        }else{
            ModelAndView mv = new ModelAndView("activeError");
            Member member = iMemberService.getById(memberid);
            mv.addObject("error","队伍暂时没有活动");
            mv.addObject("member",member);
            return mv;
        }

    }

    @PostMapping("/cancel")
    public ModelAndView cancel(@RequestParam int memberid,
                               @RequestParam int activeid){

        //这个member是用来快速返回个人页的
        Member member = iMemberService.getById(memberid);
        
        Active active = iActiveService.getById(activeid);
        if(active==null){
            ModelAndView mv = new ModelAndView("cancelError");
            mv.addObject("error","活动ID不存在！");
            mv.addObject("member",member);
            return mv;
        }

        int teamid = active.getTeamid();
        Team team = iTeamService.getById(teamid);




        //这个活动是这支队伍的，这支队伍的领队是这个人
        if(team.getLeaderid()==memberid){
            //这时才真正移除活动，else里没移除——因为没权限！
            iActiveService.removeById(active);
            ModelAndView mv = new ModelAndView("cancelSuccess");
            mv.addObject("member",member);
            return mv;
        }else{
            ModelAndView mv = new ModelAndView("cancelError");
            mv.addObject("error","您不是该活动的队伍的领队，不能取消！");
            mv.addObject("member",member);
            return mv;
        }

    }


    @PostMapping("/create")
    public ModelAndView create(@RequestParam int teamid,
                               @RequestParam int memberid,
                               @RequestParam String route,
                               @RequestParam String time,
                               @RequestParam String level){



        //把表单中的datetime-local作为String类型，但这个String缺少秒数，所以需要强转成LocalDateTime类型
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldf = LocalDateTime.parse(time.replaceAll("T", " ") + ":00", df);

        Member member = iMemberService.getById(memberid);

        //先看看用户是否为领队
        Team team = iTeamService.getById(teamid);
        if(team.getLeaderid()==memberid){
            Active active = new Active();
            active.setTeamid(teamid);
            active.setRoute(route);
            active.setTime(ldf);
            active.setLevel(level);
            iActiveService.save(active);


            //Wrapper是一个类似“条件”一样的东西
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("teamid",teamid);
            //根据条件查询返回一个Active类型的list
            List<Active> activeList = iActiveService.list(wrapper);

            ModelAndView mv = new ModelAndView("activeResult");
            mv.addObject("activelist",activeList);
            mv.addObject("team",team);
            mv.addObject("member",member);
            return mv;

        }
        //如果member对象的password是非数字，就无法被获得！
        else{
            ModelAndView mv = new ModelAndView("activeError");
            mv.addObject("error","只有领队才可以发起活动哦~ ");
            mv.addObject("member",member);
            return mv;
        }

    }

}
